package point24.pojo;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class Point24DAO {
    private static Point24DAO daoInstance = new Point24DAO();

    String resource = "mybatis-config.xml";
    InputStream inputStream = null;
    SqlSessionFactory sqlSessionFactory = null;

    private Point24DAO() {
        try {
            inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (Exception e) {
            System.err.println("数据库配置错误");
        }
    }

    public static Point24DAO getInstance() {
        return daoInstance;
    }

    public SqlSession getSession() {
        if (sqlSessionFactory == null) return null;
        return sqlSessionFactory.openSession();
    }
}
